# Copyright [2011] [Ilya Rudakov]
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from django.contrib.auth.models import User

from django.db import models

class FeedGroup(models.Model):
    name = models.CharField(max_length=50, null=False)
    mutable = models.BooleanField(null=False, default=True)

    def get_feeds(self):
        return self.feed_set.all()

    def __unicode__(self):
        return self.name

class Feed(models.Model):
    TYPE_CHOICES = (
        ("JAVA", "Java"),
        ("PYTHON", "Python")
    )

    name = models.CharField(max_length=50, null=False)
    description = models.TextField(max_length=500, null=True)
    path = models.CharField(max_length=500, null=False)
    pid = models.IntegerField(null=True)
    is_broken = models.BooleanField(null=False, default=False)
    feed_group = models.ForeignKey(FeedGroup, null=False)
    type = models.CharField(max_length=10, choices=TYPE_CHOICES)

    def __unicode__(self):
        return self.name

class Audit(models.Model):
    ACTION_TYPES = (
        ("C", "Created"),
        ("S", "Started"),
        ("I", "Interrupted"),
        ("GC", "Group Changed")
    )

    job = models.ForeignKey(Feed, null=False)
    user = models.ForeignKey(User, null=False)
    action = models.CharField(max_length=2, choices=ACTION_TYPES)
    date = models.DateTimeField(auto_now_add=True, auto_now=True)

    def __unicode__(self):
        return str(self.job) + " " + str(self.user) + " " + str(self.action) + " " + str(self.date)
